<?php


namespace App\Http\Controllers;

use App\Models\ApprovalBorrow;
use App\Services\ApprovalService;
use App\Services\ClassifyService;
use App\Services\CommonService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 审批管理类
 * Class ApprovalController
 * @package App\Http\Controllers
 */
class ApprovalController extends Controller
{
    public $classifyService;
    public $approvalService;

    public function __construct(ClassifyService $ClassifyService, ApprovalService $approvalService)
    {
        parent::__construct();

        $this->classifyService = $ClassifyService;
        $this->approvalService = $approvalService;
    }

    /**
     * 获取审批配置选择的人员树状图
     *
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:58
     */
    public function getTreeUsers() : JsonResponse
    {
        $list = $this->approvalService->getApprovalUsers();

        return $this->success($list);
    }

    /**
     * 配置审批规则
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:58
     */
    public function config(Request $request) : JsonResponse
    {
        $result = $this->approvalService->setRule($request->all());
        if ($result['msg']) {
            return $this->error($result['msg'], 400);
        }

        return $this->success('', '配置成功！');
    }

    /**
     * 删除指定审批类型
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:57
     */
    public function delRule(Request $request) : JsonResponse
    {
        $type = $request->input('type');
        if (!$this->approvalService->delApprovalRule($type)) {
            return $this->error('删除失败！');
        }
        CommonService::addActionLog('删除审批类型', "type={$type}");

        return $this->success('', '删除成功！');
    }

    /**
     * 提交案卷审批
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:57
     */
    public function submitBookTitleApproval(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $projectClassifyId = $request->input('project_classify_id');
        $bookTitleIds      = $request->input('ids');

        $result = $this->approvalService->submitBookTitleApprovalData($partCompanyId, $projectClassifyId, $bookTitleIds);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }

        return $this->success('', '提交成功！');
    }

    /**
     * 获取【案卷审批】提交数据，审批操作的用户仅能看到符合自己审批等级的待审批数据
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:56
     */
    public function getApprovalBookTitles(Request $request) : JsonResponse
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->approvalService->getApprovalBookTitles($request, $perPage);

        return $this->success($list);
    }

    /**
     * 获取【案卷审批】案卷
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:56
     */
    public function getBookTitles(Request $request) : JsonResponse
    {
        $bookTitleApprovalId = $request->input('book_title_approval_id');
        $perPage = $request->input('per_page') ?: $this->limit;
        $keyword = $request->input('keyword');

        $list = $this->approvalService->getBookTitleList($bookTitleApprovalId, $perPage, $keyword);

        return $this->success($list);
    }

    /**
     * 获取【案卷审批】卷内
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:56
     */
    public function getBookCatalogs(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $projectClassifyId = $request->input('project_classify_id');
        $bookTitleId       = $request->input('book_title_id');
        $perPage           = $request->input('per_page') ?: $this->limit;

        $list = $this->approvalService->getBookCatalogList($partCompanyId, $projectClassifyId, $bookTitleId, $request, $perPage);

        return $this->success($list);
    }

    /**
     * 案卷审批操作
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:55
     */
    public function bookTitleApprovalAction(Request $request) : JsonResponse
    {
        $params = $request->all();
        $result = $this->approvalService->bookTitleApprovalAction($params);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }

        return $this->success('', '操作成功！');
    }

    /**
     * 提交借阅审批 . 检索阶段，查看卷内PDF需要申请，而且通过后也是指定时间内可查看，过期后无法查看
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:55
     */
    public function submitBorrows(Request $request) : JsonResponse
    {
        $params = $request->input();
        $result = $this->approvalService->submitBorrowApprovalData($params);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }

        return $this->success('', '提交成功！');
    }

    /**
     * 获取借阅审批数据
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:54
     */
    public function getBorrowData(Request $request) : JsonResponse
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->approvalService->getBorrowsData($request, $perPage);

        return $this->success($list);
    }

    /**
     * 获取案卷借阅
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/1/25 9:03
     */
    public function getBorrowBookTitles(Request $request) : JsonResponse
    {
        $perPage          = $request->input('per_page') ?: $this->limit;
        $approvalBorrowId = $request->input('approval_borrow_id');
        $approvalBorrow   = ApprovalBorrow::query()->where('id', $approvalBorrowId)->first();
        if (empty($approvalBorrow)) {
            return $this->error('借阅记录不存在！');
        }
        $list = $this->approvalService->getBorrowBookTitleList($approvalBorrow, $request, $perPage);

        return $this->success($list);
    }

    /**
     * 获取【借阅审批】卷内
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:52
     */
    public function getBorrowBookCatalogs(Request $request) : JsonResponse
    {
        $partCompanyId = $request->input('part_company_id');
        $bookTitleId   = $request->input('book_title_id');
        $perPage       = $request->input('per_page') ?: $this->limit;

        $list = $this->approvalService->getBookCatalogList($partCompanyId, $bookTitleId, $request, $perPage);

        return $this->success($list);
    }

    /**
     * 借阅审批操作
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:51
     */
    public function borrowApprovalAction(Request $request) : JsonResponse
    {
        $params = $request->all();
        $result = $this->approvalService->borrowApprovalAction($params);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }

        return $this->success('操作成功！');
    }

    /**
     * 获取待审核的分类列表
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/27 13:51
     */
    public function waitAuditClassifies(Request $request) : JsonResponse
    {
        $perPage = config('site.limit') ?: 15;

        $list = $this->classifyService->getNotAuditClassifies($request, $perPage);

        return $this->success($list);
    }


}
